package com.wise.api.json.utils;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * 
 * Title: ApiJsonUtils <br>
 * Description: wiseAPI JSON 工具类
 * 
 * @author yacong_liu Email:2682505646@qq.com
 * @date 2019年4月29日
 */
public class ApiJsonUtils {

    /**
     * 
     * <p>
     * Title: parseJsonStrForBankAccountInfo
     * </p>
     * <p>
     * Description: 解析JSON串获取银行账号目前的信息
     * </p>
     * 
     * @param findRes
     * @return Map<String, String> key: BankTypeID(接口类型码) BankTypeName(接口类型名称) FAcntNumber（银行账号）
     *         FAcntName（账户名称） FAcntBranch（开户行）
     */
    public static Map<String, String> parseJsonStrForBankAccountInfo(String jsonStr) {
        StringBuffer log = new StringBuffer();
        log.append("*************ApiJsonUtils_parseJsonStrForBankAccountInfo （解析JSON串获取银行账号目前的信息）********\n");
        log.append("****入参：\n" + jsonStr);

        Map<String, String> map = new HashMap<String, String>(5);
        if (StringUtils.isEmpty(jsonStr)) {
            return map;
        }

        JSONObject parseObject = JSON.parseObject(jsonStr);
        if (checkStatus(parseObject)) {
            if (parseObject.containsKey("Data")) {
                Object data = parseObject.get("Data");
                if (data instanceof JSONArray) {
                    JSONArray arr = (JSONArray) data;
                    JSONObject jsonObj = (JSONObject) arr.get(0);
                    JSONObject item = (JSONObject) jsonObj.get("Data");
                    JSONObject bankType = item.getJSONObject("FBankTypeID");
                    map.put("BankTypeID", bankType.getString("FID"));
                    map.put("BankTypeName", bankType.getString("FName"));
                    map.put("FAcntNumber", item.getString("FAcntNumber"));
                    map.put("FAcntName", item.getString("FAcntName"));
                    map.put("FAcntBranch", item.getString("FAcntBranch"));

                    log.append("\n*********解析后银行账号信息——接口类型：" + bankType.getString("FID") + ":"
                            + bankType.getString("FName") + " 银行账号:" + item.getString("FAcntNumber")
                            + " 账户名称:" + item.getString("FAcntName") + " 开户行:"
                            + item.getString("FAcntBranch"));
                }
            }
        }

        System.out.println(log.toString());
        return map;
    }

    /**
     * 
     * <p>
     * Title: parseJsonStrForFDProperty
     * </p>
     * <p>
     * Description: 解析JSON串获取部门属性
     * </p>
     * 
     * @param jsonStr
     * @return Map<String, String> key：FID FName
     */
    public static Map<String, String> parseJsonStrForFDProperty(String jsonStr) {

        StringBuffer log = new StringBuffer();
        log.append("*************ApiJsonUtils_parseJsonStrForGroupID （解析JSON串获取部门属性）********\n");
        log.append("****入参：\n" + jsonStr);

        Map<String, String> map = new HashMap<String, String>(2);
        if (StringUtils.isEmpty(jsonStr)) {
            return map;
        }

        JSONObject parseObject = JSON.parseObject(jsonStr);
        if (checkStatus(parseObject)) {
            if (parseObject.containsKey("Data")) {
                Object data = parseObject.get("Data");
                if (data instanceof JSONArray) {
                    JSONArray arr = (JSONArray) data;
                    JSONObject jsonObj = (JSONObject) arr.get(0);
                    JSONObject item = (JSONObject) jsonObj.get("Data");
                    if (item.containsKey("FDProperty")) {
                        JSONObject property = item.getJSONObject("FDProperty");
                        map.put("FID", property.getString("FID"));
                        map.put("FName", property.getString("FName"));
                        log.append("\n*********解析后部门属性：" + property.getString("FID") + ":"
                                + property.getString("FName"));
                    }
                }
            }
        }

        System.out.println(log.toString());
        return map;
    }

    /**
     * 
     * <p>
     * Title: parseJsonStrForGroupID
     * </p>
     * <p>
     * Description: 解析JSON串获取类别代码
     * </p>
     * 
     * @param jsonStr
     * @return
     */
    public static Integer parseJsonStrForGroupID(String jsonStr) {

        Integer groupID = null;
        StringBuffer log = new StringBuffer();
        log.append("*************ApiJsonUtils_parseJsonStrForGroupID （解析JSON串获取类别代码）********\n");
        log.append("****入参：\n" + jsonStr);

        if (StringUtils.isEmpty(jsonStr)) {
            return groupID;
        }

        JSONObject parseObject = JSON.parseObject(jsonStr);
        if (checkStatus(parseObject)) {
            if (parseObject.containsKey("Data")) {
                Object data = parseObject.get("Data");
                if (data instanceof JSONArray) {
                    JSONArray arr = (JSONArray) data;
                    JSONObject jsonObj = (JSONObject) arr.get(0);
                    JSONObject item = (JSONObject) jsonObj.get("Data");
                    groupID = item.getInteger("FGroupID");
                    log.append("\n*********解析后科目类别代码：" + groupID);
                }
            }
        }

        System.out.println(log.toString());
        return groupID;

    }

    /**
     * 
     * <p>
     * Title: getFNumberByFilter
     * </p>
     * <p>
     * Description: 解析JSON串获取编码
     * </p>
     * 
     * @param filterJson
     * @return
     */
    public static String parseJsonStrForNumber(String filterJson) {

        String fNumber = "";
        StringBuffer log = new StringBuffer();
        log.append("*************ApiJsonUtils_parseJsonStrForNumber （解析JSON获取编码）********\n");
        log.append("****入参：\n" + filterJson);
        if (StringUtils.isEmpty(filterJson)) {
            return fNumber;
        }

        JSONObject jsonObj = JSON.parseObject(filterJson);
        if (checkStatus(jsonObj)) {
            if (jsonObj.containsKey("Data")) {
                Object object = jsonObj.get("Data");
                JSONObject dataObj = JSON.parseObject(JSONObject.toJSONString(object));
                Object data = dataObj.get("Data");

                if (data instanceof JSONArray) {
                    JSONArray arr = (JSONArray) data;
                    JSONObject item = (JSONObject) arr.get(0);
                    // 获取到基础资料编码
                    fNumber = item.getString("FNumber");
                    log.append("\n*********解析后编码：" + fNumber);
                }
            }
        }

        System.out.println(log.toString());
        return fNumber;

    }

    public static boolean checkStatus(String json) {
        return checkStatus(JSON.parseObject(json));

    }

    public static boolean checkStatus(JSONObject jsonObj) {

        boolean flag = false;
        if (!jsonObj.isEmpty()) {
            flag = (jsonObj.containsKey("StatusCode") && jsonObj.getInteger("StatusCode") == 200);
        }

        return flag;
    }

}
